package com.voicechat.firstmet.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.fragment.SearchFragment;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.LazyViewPager;
import com.voicechat.firstmet.view.SimpleViewPagerIndicatorLook;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity
 * @ClassName: SearchShowActivity
 * @Description: java类作用描述
 * @Author: ljp
 * @CreateDate: 2018/11/16 14:14
 * @Version: 1.0
 */
public class SearchShowActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.indicator_search)
    SimpleViewPagerIndicatorLook indicatorSearch;
    @BindView(R.id.viewpager_search)
    LazyViewPager viewpagerSearch;
    @BindView(R.id.linear_search_fragment)
    LinearLayout linearSearchFragment;
    private String[] mTitles;
    private ArrayList<Fragment> fragments;
    private String token;
    private String deviceInfo;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String userId;
    private MyAdapter mAdapter;
    private String search;

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_search_show;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        userId = mySeleInfo.getId();
        search = getIntent().getStringExtra("search");
        initView();
    }

    private void initView() {
        mTitles = getResources().getStringArray(R.array.search_list);
        initIndicator();
        bindEvent();
        initViewPager();
        etSearch.setText(search);
        Utils.hideSoftInput(etSearch.getWindowToken(),context);
        etSearch.clearFocus();
    }
    /**
     * @return
     * @method
     * @description viewpager与fragment绑定
     * @date: 2018/10/11 16:40
     * @author: ljp
     */
    private void initViewPager() {
        fragments = new ArrayList<>();
        fragments.add(SearchFragment.newInstance("user",search));
        fragments.add(SearchFragment.newInstance("post",search));
        mAdapter = new MyAdapter(getSupportFragmentManager());
        viewpagerSearch.setAdapter(mAdapter);
    }
    /**
     * @return
     * @method
     * @description 标题滑动监听
     * @date: 2018/10/11 16:40
     * @author: ljp
     */
    private void bindEvent() {
        indicatorSearch.setOnPageChangeListener(new SimpleViewPagerIndicatorLook.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                indicatorSearch.setCurrentItem(position);
            }
        });
    }
    @OnClick({R.id.linear_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
               //startActivity(new Intent(activity,SearchActivity.class));
               finish();
                break;
        }
    }
    /**
     * @return
     * @method
     * @description fragment切换标题数据
     * @date: 2018/10/11 16:42
     * @author: ljp
     */
    private void initIndicator() {
        if (null == mTitles || mTitles.length == 0) {
            indicatorSearch.setVisibility(View.GONE);
        } else {
            indicatorSearch.setTitles(mTitles);
            indicatorSearch
                    .setViewPager(viewpagerSearch);
            viewpagerSearch.setCurrentItem(0);
        }
    }
    private class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }
        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return fragments.get(arg0);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return fragments.size();
        }

    }
}
